/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.pharmacy.service.impl;

import me.zhengjie.modules.his.domain.Pharmacy;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.pharmacy.repository.PharmacyRepository;
import me.zhengjie.modules.his.pharmacy.service.PharmacyService;
import me.zhengjie.modules.his.pharmacy.service.dto.PharmacyDto;
import me.zhengjie.modules.his.pharmacy.service.dto.PharmacyQueryCriteria;
import me.zhengjie.modules.his.pharmacy.service.mapstruct.PharmacyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class PharmacyServiceImpl implements PharmacyService {

    private final PharmacyRepository pharmacyRepository;
    private final PharmacyMapper pharmacyMapper;

    @Override
    public Map<String,Object> queryAll(PharmacyQueryCriteria criteria, Pageable pageable){
        Page<Pharmacy> page = pharmacyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(pharmacyMapper::toDto));
    }

    @Override
    public List<PharmacyDto> queryAll(PharmacyQueryCriteria criteria){
        return pharmacyMapper.toDto(pharmacyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PharmacyDto findById(Integer pharmacyId) {
        Pharmacy pharmacy = pharmacyRepository.findById(pharmacyId).orElseGet(Pharmacy::new);
        ValidationUtil.isNull(pharmacy.getPharmacyId(),"Pharmacy","pharmacyId",pharmacyId);
        return pharmacyMapper.toDto(pharmacy);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PharmacyDto create(Pharmacy resources) {
        return pharmacyMapper.toDto(pharmacyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Pharmacy resources) {
        Pharmacy pharmacy = pharmacyRepository.findById(resources.getPharmacyId()).orElseGet(Pharmacy::new);
        ValidationUtil.isNull( pharmacy.getPharmacyId(),"Pharmacy","id",resources.getPharmacyId());
        pharmacy.copy(resources);
        pharmacyRepository.save(pharmacy);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer pharmacyId : ids) {
            pharmacyRepository.deleteById(pharmacyId);
        }
    }

    @Override
    public void download(List<PharmacyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PharmacyDto pharmacy : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药品名称", pharmacy.getPharmacyName());
            map.put("领货单位", pharmacy.getDrugstoreId());
            map.put("经办人", pharmacy.getSkullId());
            map.put("库房", pharmacy.getWarehouseId());
            map.put("计量单位", pharmacy.getUnit());
            map.put("售价", pharmacy.getSellingPrice());
            map.put("产地", pharmacy.getArea());
            map.put("类型", pharmacy.getType());
            map.put("生产日期", pharmacy.getProduceDate());
            map.put("有效期", pharmacy.getValidDate());
            map.put("数量", pharmacy.getDrugstoreNum());
            map.put("批号", pharmacy.getSkullBatch());
            map.put("创建者", pharmacy.getCreateBy());
            map.put("更新者", pharmacy.getUpdateBy());
            map.put("创建日期", pharmacy.getCreateTime());
            map.put("更新时间", pharmacy.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}